#include "font.h"
#include "rfid.h"
#include "beep.h"
static struct LcdDevice* lcd;
char font_txt[128] ={0};
int i;
int j;
int move_flag =0;
//1、打开lcd显示屏(字库)
void Font_Lcd_Open(void)
{
	//申请空间
	lcd = malloc(sizeof(struct LcdDevice));
	if(lcd == NULL){
		return ;
	} 
	//1打开设备
	lcd->fd = open("/dev/fb0", O_RDWR);
	if(lcd->fd < 0){
		perror("open lcd fail");
		free(lcd);
		return ;
	}
	//映射
	lcd->mp = mmap(NULL,800*480*4,PROT_READ|PROT_WRITE,MAP_SHARED,lcd->fd,0);

	return ;
}


void *Font_Lcd_Show_Pt(void *arg)
{
	Bmp_Show(0, 0, 800, 480, "./white.bmp");
	while(1)
	{
		if (page == page_main)
		{
			if (rfid_card_id > 0)						//检测到卡，打印出卡号
		 	{
		 		bzero(font_txt, sizeof(font_txt));
				sprintf(font_txt, "rfid_card_id = %#x\n", rfid_card_id);
		 		move_flag =1;
		 		rfid_card_id = 0;
		 		BEEP_Button(1);
		 		sleep(1);
		 		BEEP_Button(0);
			}
			Font_Lcd_Show(50, 430, 80, i, 0, font_txt, 178, 405,255,255,255);
			i++;
			if (i>=800||rfid_card_id > 0)				//检测到卡号从头开始滚动
			{
				i=0;
			}
		}
	}
	
	pthread_exit(NULL);
	return (void *)0;
}
void num_green(int num)
{
	if( num==0)
		Font_Lcd_Show(50, 125, 95, 62, 47, "1", 654, 246,0  ,255,0);
	else  if( num==1)
		Font_Lcd_Show(50, 125, 95, 62, 47, "2", 654, 131,0  ,255,0);
	else  if( num==2)
		Font_Lcd_Show(50, 125, 95, 62, 47, "3", 654, 16 ,0  ,255,0);
	else  if( num==3)
		Font_Lcd_Show(50, 125, 95, 62, 47, "4", 481, 16 ,0  ,255,0);
	else  if( num==4)
		Font_Lcd_Show(50, 125, 95, 62, 47, "5", 328, 16 ,0  ,255,0);
	else  if( num==5)
		Font_Lcd_Show(50, 125, 95, 62, 47, "6", 176, 16 ,0  ,255,0);
	else  if( num==6)
		Font_Lcd_Show(50, 125, 95, 62, 47, "7", 22 , 16 ,0  ,255,0);
	else  if( num==7)
		Font_Lcd_Show(50, 125, 95, 62, 47, "8", 22 , 132,0  ,255,0);

}

void lcd_show_roll(int num)//终点
{
	int i=0;
	int last_i=10;
	switch (0)
	{
		printf("aa\n");
		case 0:
			if (cell[i] !=2)
			{
			sleep(1);
			Font_Lcd_Show(50, 125, 95, 62, 47, "1", 654, 246,255,255,0);
			last_i=i;
			}
			// if (num ==0)	cell[i] =2;		
			if(i>=num) 
			{
				cell[i] =2;
				camera_photo_flag =1;
				break;
			}
			i++;

		case 1:
			if (cell[i] !=2)
			{
				if (cell[i-1] !=2)
				{
					sleep(1);
					Font_Lcd_Show(50, 125, 95, 62, 47, "1", 654, 246,0  ,255,0);
				}
				sleep(1);
				num_green(last_i);			
				sleep(1);
				Font_Lcd_Show(50, 125, 95, 62, 47, "2", 654, 131,255,255,0);
				last_i=i;
			}
			if(i>=num) 
			{
				cell[i] =2;
				camera_photo_flag =1;
				break;
			}
			i++;

		case 2:
			if (cell[i] !=2)
			{
				if (cell[i-1] !=2)
				{
					sleep(1);
					Font_Lcd_Show(50, 125, 95, 62, 47, "2", 654, 131,0  ,255,0);
				}
				sleep(1);
				num_green(last_i);			
				sleep(1);
				Font_Lcd_Show(50, 125, 95, 62, 47, "3", 654, 16 ,255,255,0);

				last_i=i;
			}
			if(i>=num) 
			{
				cell[i] =2;
				camera_photo_flag =1;
				break;
			}
			i++;

		case 3:
			if (cell[i] !=2)
			{
				if (cell[i-1] !=2)
				{
					sleep(1);
					Font_Lcd_Show(50, 125, 95, 62, 47, "3", 654, 16 ,0  ,255,0);
				}	
				sleep(1);
				num_green(last_i);			
				sleep(1);
				Font_Lcd_Show(50, 125, 95, 62, 47, "4", 481, 16 ,255,255,0);
				last_i=i;
			}
			if(i>=num) 
			{
				printf("bb\n");
				cell[i] =2;
				camera_photo_flag =1;
				break;
			}
			i++;

		case 4:
			if (cell[i] !=2)
			{
				if (cell[i-1] !=2)
				{
					sleep(1);
					Font_Lcd_Show(50, 125, 95, 62, 47, "4", 481, 16 ,0  ,255,0);
				}	
				sleep(1);
				num_green(last_i);			
				sleep(1);
				Font_Lcd_Show(50, 125, 95, 62, 47, "5", 328, 16 ,255,255,0);
				last_i=i;
			}
			if(i>=num) 
			{
				cell[i] =2;
				camera_photo_flag =1;
				break;
			}
			i++;

		case 5:
			if (cell[i] !=2)
			{
				if (cell[i-1] !=2)
				{
					sleep(1);
					Font_Lcd_Show(50, 125, 95, 62, 47, "5", 328, 16 ,0  ,255,0);
				}	
				sleep(1);
				num_green(last_i);
				sleep(1);
				Font_Lcd_Show(50, 125, 95, 62, 47, "6", 176, 16 ,255,255,0);
				last_i=i;
			}
			if(i>=num) 
			{
				cell[i] =2;
				camera_photo_flag =1;
				break;
			}
			i++;

		case 6:
			if (cell[i] !=2)
			{
				if (cell[i-1] !=2)
				{
					sleep(1);
					Font_Lcd_Show(50, 125, 95, 62, 47, "6", 176, 16 ,0  ,255,0);
				}	
				sleep(1);
				num_green(last_i);
				sleep(1);
				Font_Lcd_Show(50, 125, 95, 62, 47, "7", 22 , 16 ,255,255,0);
				last_i=i;
			}
			if(i>=num) 
			{
				cell[i] =2;
				camera_photo_flag =1;
				break;
			}
			i++;


		case 7:
			if (cell[i] !=2)
			{
				if (cell[i-1] !=2)
				{
					sleep(1);
					Font_Lcd_Show(50, 125, 95, 62, 47, "7", 22 , 16 ,0  ,255,0);
				}	
				sleep(1);
				num_green(last_i);
				sleep(1);
				Font_Lcd_Show(50, 125, 95, 62, 47, "8", 22 , 132,255,255,0);
				last_i=i;
			}
			if(i>=num) 
			{
				cell[i] =2;
				camera_photo_flag =1;
				break;
			}
			i++;

		case 8:
			if (cell[i] !=2)
			{
				if (cell[i-1] !=2)
				{
					sleep(1);
					Font_Lcd_Show(50, 125, 95, 62, 47, "8", 22 , 132,0  ,255,0);
				}	
				sleep(1);
				num_green(last_i);
				sleep(1);
				Font_Lcd_Show(50, 125, 95, 62, 47, "9", 22 , 247,255,255,0);
				last_i=i;
			}
			if(i>=num) 
			{
			cell[i] =2;
			camera_photo_flag =1;
			break;
			}
			i++;
	}

}

void *Lcd_Pt (void *arg)
{
	char    jpg_photo[128]  =  {0}  ; 			//3、拍摄照片路径数组(六)
	int     jpg_photo_num	=	0	;			//4、拍摄照数量计数(六)
	Bmp_Show(0, 0, 800, 480, "./white.bmp");
	Font_Lcd_Show(50, 125, 95, 62, 47, "1", 654, 246,0  ,255,0);
	Font_Lcd_Show(50, 125, 95, 62, 47, "2", 654, 131,0  ,255,0);
	Font_Lcd_Show(50, 125, 95, 62, 47, "3", 654, 16 ,0  ,255,0);
	Font_Lcd_Show(50, 125, 95, 62, 47, "4", 481, 16 ,0  ,255,0);
	Font_Lcd_Show(50, 125, 95, 62, 47, "5", 328, 16 ,0  ,255,0);
	Font_Lcd_Show(50, 125, 95, 62, 47, "6", 176, 16 ,0  ,255,0);
	Font_Lcd_Show(50, 125, 95, 62, 47, "7", 22 , 16 ,0  ,255,0);
	Font_Lcd_Show(50, 125, 95, 62, 47, "8", 22 , 132,0  ,255,0);
	Font_Lcd_Show(50, 125, 95, 62, 47, "9", 22 , 247,0  ,255,0);
	Font_Lcd_Show(30, 125, 95, 25, 40, "操作页面",22,362,0,255,0);
	while(1)
	{
		if(page==page_main)
		{
			if (setauto==0)
			{
				for (j =0; j < 9; j++)
				{
					if (cell[j] ==1)
					{
						printf("%d\n",j);
						lcd_show_roll(j);
					}
				}
			}
			else if (setauto==1)
			{
				if ( move_flag==1)
				{
					for (j =0; j < 9; j++)
					{
						if (cell[j]!=2)
						{
							lcd_show_roll(j);
							move_flag = 0;
							break;
						}
					}
				}
			}

		
		}
			else if(page==page_photo) 
		{

			if (ts_x>0  && ts_x<82 && ts_y>219 && ts_y<297 && ts_touch==1)
			{
				jpg_photo_num++;
				if (jpg_photo_num>9)
				jpg_photo_num = 0;	
			}
			else if (ts_x>712  && ts_x<800 && ts_y>223 && ts_y<305 && ts_touch==1)
			{
				jpg_photo_num--;
				if (jpg_photo_num<1)
				jpg_photo_num = 9;
			}
			bzero(jpg_photo, sizeof(jpg_photo));
			sprintf(jpg_photo, "./photo/%d.jpg", jpg_photo_num);
			Jpeg_Lcd_Show(86, 0, jpg_photo);	
		}
	}
	
	pthread_exit(NULL);
	return (void *)0;
}
/*		
	2、显示字(可以指定字体大小，画板的x轴长度，画板的y轴长度，字在画板的x轴位置， 
       字在画板的y轴位置，文本，画在lcd的x轴位置，画在lcd的y轴位置，)
*/
void Font_Lcd_Show(s32 size, u32 bm_x, u32 bm_y, s32 f_bm_x,
                   s32 f_bm_y, char *text, int lcd_x, int lcd_y,int r,int g, int b)
{			
	//1、打开字体	
	font *f = fontLoad("/usr/share/fonts/DroidSansFallback.ttf");
	  
	//2、字体大小的设置
	fontSetSize(f,size);
	
	//3、创建一个画板（点阵图）
	bitmap *bm = createBitmapWithInit(bm_x,bm_y,4,getColor(0,b, g, r));
	                                                    //画板的颜色ABGR
                
	//4、将字体画到画板上(点阵图上)
	fontPrint(f,bm,f_bm_x,f_bm_y,text,getColor(0,0,0,0),0);
							//字体的颜色ABGR
	
	//5、把形成的画显示到LCD屏幕上
	show_font_to_lcd(lcd->mp,lcd_x,lcd_y,bm);

	//6、关闭字体，关闭画板
	fontUnload(f);
	destroyBitmap(bm);
		
}


//3、打开lcd显示屏(字库)
void Font_Lcd_Close(void)
{
	//1、解除lcd映射
	int ret_m = munmap(lcd->mp, 800*480*4);
	if (-1 == ret_m){
		perror("munmap lcd error!\n");
		goto Label;
	}
	//2、关闭lcd显示屏文件
	int ret_c = close(lcd->fd);
	if (-1 == ret_c){
		perror("close lcd error!\n");
		goto Label;
	}	
Label:
	munmap(lcd->mp, 800*480*4);
	close(lcd->fd);

}

